<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="file" id="inputFile" style="display:none" accept="image/png,image/jpeg">
    <button>点击上传</button> <br>
    <img src="" alt="">
    <script>
        const btn = document.querySelector('button')
        const inputFile = document.querySelector('#inputFile')
        const img = document.querySelector('img')
        btn.addEventListener('click', () => {
            inputFile.click()
        })
        inputFile.addEventListener('change', () => {
            const file = inputFile.files[0]
            console.log(file)
            const formDate = new FormData()
            formDate.append('avatar', file)
            console.log(formDate)
            fetch('https://autumnfish.cn/api/form/upload', {
                method: 'POST',
                body: formDate
            })
                .then(res => res.json())
                .then(res => {
                    img.src = res.data
                })
        })


        // btn.addEventListener('click', () => {
        //     inputFile.click()
        // })
        // inputFile.onchange = e => {
        //     const [file] = e.target.files
        //     const formDate = new FormData()
        //     formDate.append('avatar', file)
        //     fetch('https://autumnfish.cn/api/form/upload', {
        //         method: 'POST',
        //         body: formDate
        //     })
        //         .then(res => res.json())
        //         .then(res => {
        //             img.setAttribute('src', res.data)
        //         })
        // }
        // const btn = document.querySelector('button')
        // const inputFile = document.querySelector('#inputFile')
        // const img = document.querySelector('img')
        // btn.onclick = () => {
        //     inputFile.click()
        // }
        // inputFile.onchange = e => {
        //     const [file] = e.target.files
        //     const formDate = new FormData()
        //     formDate.append('avatar', file)
        //     fetch('https://autumnfish.cn/api/form/upload', {
        //         method: 'POST',
        //         body: formDate
        //     })
        //         .then(res => res.json())
        //         .then(res => {
        //             img.setAttribute('src', res.data)
        //         })
        // }
    </script>
</body>

</html>